/**
 * Created by wangcan on 2017/12/15.
 */

$(document).ready(function(){

    getData();

});

function getData(){

    //获取url中的参数
    var token = $.getUrlParam('token');
    var balanceId = $.getUrlParam('balanceId');
    var deviceId = $.getUrlParam("deviceId");
    var host = window.location.host;
    if(deviceId == null || deviceId == ""){
        deviceId = -1;
    }

    var url = 'http://'+ host +"/rest/v1.0/commission/balanceHistoryDetail/" + balanceId + "/" + token +"/" + deviceId;

    $.get(url, function(data, status){


        if(data.result.status == 0){

            //系统正在维护，不提供数据服务
            $(".wrap").html("<div style=\'font-size: 0.4rem; margin-top: 100px; text-align: center'>"+data.result.message+"</div>");

            if(data.result.message == "登录已过期，请重新登录"){
                try{
                    loginAction();
                }
                catch(error){
                    console.log(error);
                }
            }
            return;
        }

        // $("#wait_close").text(fixedNum(data.data.close_actuarial.wait_close)+"元"); //待结算
        // $("#wait_actuarial").text(fixedNum(data.data.close_actuarial.wait_actuarial)+"元"); //待精算

        $("#my_shop_brokerage_count_sum").text(fixedNum(data.data.my_shop_brokerage.count_sum)); //我的购物返佣--订单金额
        $("#my_shop_brokerage_count_mun").text(fixedNum(data.data.my_shop_brokerage.count_mun)); //我的购物返佣--订单数量
        $("#my_shop_brokerage_count_brokerage").text(fixedNum(data.data.my_shop_brokerage.count_brokerage)); //我的购物返佣--佣金

        $("#shared_shop_brokerage_count_sum").text(fixedNum(data.data.shared_shop_brokerage.count_sum));//分享购物返佣--订单金额
        $("#shared_shop_brokerage_count_num").text(fixedNum(data.data.shared_shop_brokerage.count_mun));//分享购物返佣--订单数量
        $("#shared_shop_brokerage_count_brokerage").text(fixedNum(data.data.shared_shop_brokerage.count_brokerage));//分享购物返佣--佣金

        $("#community_brokerage_count_sum").text(fixedNum(data.data.community_brokerage.count_sum));//我的小区返佣--订单金额
        $("#community_brokerage_count_mun").text(fixedNum(data.data.community_brokerage.count_mun));//我的小区返佣--订单数量
        $("#community_brokerage_count_brokerage").text(fixedNum(data.data.community_brokerage.count_brokerage));//我的小区返佣--佣金


        if(data.data.team_brokerage){
            var count_num = 0;  //订单总数
            var count_commission = 0;  //订单总金额

            var tempOrderIdArray = [];
            data.data.team_brokerage.forEach(function(item, index){
                var mun = item.num;
                var commission = item.commission;
                var nickName = item.nickName;

                $("#team_brokerage_man").after("<p class='color_black'>"+nickName+"</p>"); //大使
                $("#team_brokerage_num").after("<p class='color_black'>"+fixedNum(mun)+"</p>"); //数量
                $("#team_brokerage_sum").after("<p >"+fixedNum(commission)+"</p>"); //佣金

                count_num += mun;
                tempOrderIdArray.push(item.order_id);
                count_commission+= commission;
            });

            // if(tempOrderIdArray.length > 0) {
            //     var tempOrderIdCount = arrayDislodgeRepeat(tempOrderIdArray).length;
            //     count_num = tempOrderIdCount;
            // }

            $("#team_brokerage_count_num").text(fixedNum(count_num));
            $("#team_brokerage_count_sum").text(fixedNum((count_commission)));
        }
    });
}

(function ($) {
    $.getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
})(jQuery);

function fixedNum(num){
    num = num+"";
    if(!num){
        return 0;
    }

    if(num == ""){
        return 0;
    }

    if(num.indexOf(".")<=0){
        return num;
    }

    if( (num.length - num.indexOf(".")) >=3){
        var a =  Number(num);
        return a.toFixed(2);
    }
    else{
        return num;
    }
}

function arrayDislodgeRepeat(tempArr) {
    var retArr = [];
    var tempObj = {};
    for (var i = 0, length = tempArr.length; i < length; i++) {
        if (!tempObj[tempArr[i].toString()]) {
            retArr.push(tempArr[i]);
            tempObj[tempArr[i].toString()] = 1;
        }
    }
    return retArr;
};

// 结算历史订单一览页面
// function goToCommissionOrderList(tempCla) {
//     if (tempCla == "team" ) {
//         window.location.href = "teamCommissionOrderList.html" + window.location.search + '&tempCla=' + tempCla;
//     } else {
//         window.location.href = "commissionOrderList.html" + window.location.search + '&tempCla=' + tempCla;
//     }
// };